﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.IO;
using Blueprints.Resolution;
using Blueprints.Validation;
using System;
using System.Collections.Generic;

namespace Blueprints.Editing
{
    /// <summary>
    /// The context within which blueprint nodes can be edited.
    /// </summary>
    public sealed partial class EditContext
    {
        /// <summary>
        /// Occurs when the blueprint has chaged.
        /// </summary>
        public event EventHandler<BlueprintChangedEventArgs> BlueprintChanged;

        /// <summary>
        /// Creates a new EditContext.
        /// </summary>
        /// <param name="validationProvider">Provides validation for the edit nodes.</param>
        /// <param name="blueprintResolver">The blueprint resolver used to load and save blueprint documents.</param>
        public EditContext(IValidationProvider validationProvider, IBlueprintResolver blueprintResolver)
        {
            ValidationProvider = validationProvider;
            BlueprintResolver = blueprintResolver;
            New(typeof(object).AssemblyQualifiedName);
        }

        /// <summary>
        /// Gets the root node of the blueprint.
        /// </summary>
        public EditNode RootNode
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the blueprint resolver.
        /// </summary>
        public IBlueprintResolver BlueprintResolver
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the validation provider.
        /// </summary>
        public IValidationProvider ValidationProvider
        {
            get;
            private set;
        }

        internal MediaType MediaType
        {
            get;
            private set;
        }

        internal Uri DocumentUri
        {
            get;
            private set;
        }

        internal NodeCollection Nodes
        {
            get;
            private set;
        }

        /// <summary>
        /// Clears the cache, causing external documents to be inspected again.
        /// </summary>
        public void ClearCache()
        {
            ValidationProvider.ClearCache();
        }

        internal void OnBlueprintChanged(EditNode node)
        {
            EventHandler<BlueprintChangedEventArgs> handler = BlueprintChanged;
            if (handler == null)
            {
                return;
            }

            handler(this, new BlueprintChangedEventArgs(node));
        }

        private static EditNode GetRootNode(IEnumerable<EditNode> nodes)
        {
            foreach (EditNode candidate in nodes)
            {
                if (candidate.TraceableNode.NodeType != NodeType.StartNode)
                {
                    continue;
                }

                return candidate;
            }

            return null;
        }
    }
}